package com.yycx.module.bus.provider.handler.haiying.settle.calculate;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.flowable.variable.api.persistence.entity.VariableInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 已核算单列表
 *
 * @author zyf
 */
@Component
public class HyCalculateOrderTab2ListHandler implements PageInterceptor<HyOrder> {


    @Autowired
    private TaskService taskService;

    @Autowired
    protected RuntimeService runtimeService;

    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery<HyOrder> cq, PageParams pageParams, EntityMap params) {
        OpenUser openUser = OpenHelper.getUser();
        String userName = openUser.getUsername();
        String beginDate = cq.getParams("beginDate");
        String endDate = cq.getParams("endDate");
        String orderNo = cq.getParams("orderNo");
        cq.eq(HyOrder.class, "saleManName");
        cq.eq(HyOrder.class, "nickName");
        cq.eq(HyOrder.class, "deptId");
        if(FlymeUtils.isNotEmpty(orderNo)) {
            cq.and(e -> e.likeLeft("ho.orderNo", orderNo).or().likeLeft("ho.outOrderNo", orderNo));
        }
        if (FlymeUtils.isNotEmpty(beginDate)) {
            cq.ge("sendDate", beginDate);
            cq.le("sendDate", endDate);
        }
        cq.eq(BusDept.class,"deptType");
        //非管理员
        if (!userName.contains("admin")) {

        }
        cq.eq("ho.companyId", openUser.getCompanyId());
        //已审批
        cq.eq("orderState", 2);
        //已发货
        cq.eq("sendState", 1);
        //未核算
        cq.in("calculateState", 1, 2);
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<HyOrder> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("applyIncId");
            if (FlymeUtils.isNotEmpty(processInstanceId)) {
                Date sendDate = entityMap.get("sendDate");
                Long deptId = entityMap.get("deptId");
                try {
                    VariableInstance variableInstance = runtimeService.getVariableInstance(processInstanceId, "sendDate");
                    if (FlymeUtils.isEmpty(variableInstance)) {
                        runtimeService.setVariable(processInstanceId, "sendDate", DateUtils.formatDate(sendDate));
                    }
                    VariableInstance deptvar = runtimeService.getVariableInstance(processInstanceId, "deptId");
                    if (FlymeUtils.isEmpty(deptvar)) {
                        runtimeService.setVariable(processInstanceId, "deptId", deptId.toString());
                    }
                } catch (Exception e) {

                }
                //查询当前审批节点
                Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).active().singleResult();
                if (FlymeUtils.isNotEmpty(task)) {
                    entityMap.put("taskName", task.getName().replace("\n", ""));
                    entityMap.put("taskId", task.getId());
                    entityMap.put("taskDefinitionKey", task.getTaskDefinitionKey());
                }
            }
        }
        return result;
    }

}
